/* GUI CONVERTED from rssup.p (converter v1.76) Thu May 30 14:01:44 2002 */
/* rssup.p - Release Management Supplier Schedules * SCHEDULE UPDATE *        */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                        */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Revision: 1.28 $                                                  */
/* REVISION: 7.3      LAST MODIFIED: 12/10/92   BY: WUG *G462*                */
/* REVISION: 7.3      LAST MODIFIED: 03/22/93   BY: WUG *G853*                */
/* REVISION: 7.3      LAST MODIFIED: 04/16/93   BY: WUG *G963*                */
/* REVISION: 7.3      LAST MODIFIED: 06/03/93   BY: WUG *GA75*                */
/* REVISION: 7.3      LAST MODIFIED: 06/17/93   BY: WUG *GC40*                */
/* REVISION: 7.4      LAST MODIFIED: 09/29/93   BY: WUG *H142*                */
/* REVISION: 7.4      LAST MODIFIED: 01/20/94   BY: WUG *GI51*                */
/* REVISION: 7.3      LAST MODIFIED: 01/24/94   BY: WUG *GG88*                */
/* REVISION: 7.3      LAST MODIFIED: 04/05/94   BY: WUG *GJ27*                */
/* REVISION: 7.3      LAST MODIFIED: 05/16/94   BY: WUG *GJ59*                */
/* REVISION: 7.3      LAST MODIFIED: 10/20/94   BY: dpm *GN13*                */
/* REVISION: 7.4      LAST MODIFIED: 11/09/94   BY: WUG *GN76*                */
/* REVISION: 7.4      LAST MODIFIED: 03/10/95   BY: rxm *G0GZ*                */
/* REVISION: 7.4      LAST MODIFIED: 03/29/95   BY: dzn *F0PN*                */
/* REVISION: 7.4      LAST MODIFIED: 04/06/95   BY: ame *H0CF*                */
/* REVISION: 7.4      LAST MODIFIED: 04/21/95   BY: bcm *G0KS*                */
/* REVISION: 7.3      LAST MODIFIED: 05/15/95   BY: WUG *G0MV*                */
/* REVISION: 7.3      LAST MODIFIED: 10/18/95   BY: vrn *G0ZW*                */
/* REVISION: 7.4      LAST MODIFIED: 03/01/96   BY: kjm *G1P5*                */
/* REVISION: 8.5      LAST MODIFIED: 02/28/96   BY: *J0CV* Brandy J Ewing     */
/* REVISION: 8.5      LAST MODIFIED: 08/06/96   BY: *G2BF* Sanjay Patil       */
/* REVISION: 8.6      LAST MODIFIED: 04/24/97   BY: *K0CJ* Vinay Nayak-Sujir  */
/* REVISION: 8.6E     LAST MODIFIED: 02/23/98   BY: *L007* A. Rahane          */
/* REVISION: 8.6E     LAST MODIFIED: 05/20/98   BY: *K1Q4* Alfred Tan         */
/* REVISION: 8.6E     LAST MODIFIED: 07/15/98   BY: *L041* Manish K.          */
/* REVISION: 8.6E     LAST MODIFIED: 08/19/98   BY: *L062* Steve Nugent       */
/* REVISION: 8.6E     LAST MODIFIED: 10/04/98   BY: *J314* Alfred Tan         */
/* REVISION: 9.0      LAST MODIFIED: 12/02/98   BY: *K1QY* Suresh Nayak       */
/* REVISION: 9.0      LAST MODIFIED: 03/13/99   BY: *M0BD* Alfred Tan         */
/* REVISION: 9.1      LAST MODIFIED: 08/10/99   BY: *N01B* Mugdha Tambe       */
/* REVISION: 9.1      LAST MODIFIED: 11/15/99   BY: *N05K* Kedar Deherkar     */
/* REVISION: 9.1      LAST MODIFIED: 03/24/00   BY: *N08T* Annasaheb Rahane   */
/* REVISION: 9.1      LAST MODIFIED: 06/06/00   BY: *N0CX* Jean Miller        */
/* REVISION: 9.1      LAST MODIFIED: 08/12/00   BY: *N0KP* Mark Brown         */
/* REVISION: 9.1      LAST MODIFIED: 10/06/00   BY: *N0RT* Jean Miller        */
/* Revision: 1.22       BY: Robin McCarthy       DATE: 10/01/01  ECO: *P025*  */
/* Revision: 1.23       BY: Anitha Gopal         DATE: 02/17/02  ECO: *N19G*  */
/* Revision: 1.24       BY: Jean Miller          DATE: 04/03/02  ECO: *P053*  */
/* Revision: 1.25       BY: Vinod Nair           DATE: 04/22/02  ECO: *N1GV*  */
/* Revision: 1.26       BY: Mercy C.             DATE: 04/25/02  ECO: *N1H5*  */
/* Revision: 1.27       BY: Mercy C.             DATE: 05/13/02  ECO: *N1J6*  */
/* $Revision: 1.28 $      BY: Dan Herman         DATE: 05/24/02  ECO: *P018*  */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

/*V8:ConvertMode=Report                                                       */


/*GUI preprocessor directive settings */
&SCOPED-DEFINE PP_GUI_CONVERT_MODE REPORT

{mfdtitle.i "2+ "}

/* PREPROCESSOR USED FOR REPORT'S WITH SIMULATION OPTION */
&SCOPED-DEFINE simulation true

define variable supp_schedule as integer initial 4.
define variable shipto_from like po_ship.
define variable shipto_to   like po_ship.
define variable part_from like pod_part.
define variable part_to   like pod_part.
define variable supplier_from like po_vend.
define variable supplier_to   like po_vend.
define variable po_from like po_nbr.
define variable po_to   like po_nbr.
define variable report_yn like mfc_logical label "Report Detail/Summary"
   format "Detail/Summary".
/* DEFAULT VALUE FOR UPDATE_YN IS CHANGED FROM YES TO NO */
/* SINCE IT IS RECOMMENDED APPROACH IN HELP DOCUMENTATION */
define variable update_yn like mfc_logical label "Update" no-undo.
define variable asof_date as date label "As-of Date".
define variable work_qty like schd_discr_qty.
define variable prior_end_date as date.
define variable new_end_date as date.
define variable cum_qty like sch_pcr_qty.
define variable ord_chg as decimal.
define variable start_date as date.
define variable orig_cum_qty as decimal.
define variable new_cum_qty as decimal.
define variable work_diff as decimal.
define variable i as integer.
define variable req_cum_req_qty like schd_cum_qty.
define variable monday_date as date.
define variable shipfrom_date as date.
define variable shipfrom_time as character.
define variable ret_status as character.
define variable interval as character.
define variable work_dt as date.
define variable last_dt as date.
define variable past_logic as integer.
define variable warn_ct as integer no-undo.
define variable error_ct as integer no-undo.
define new shared stream audit.
define variable sch_recid as recid.
define variable daylabel as character format "x(10)".
define variable buyer_from like po_buyer.
define variable buyer_to like po_buyer.
define variable past_due as decimal.
define variable amt_to_reduce like schd_det.schd_discr_qty.
define variable sched_type like sch_type.
define variable trans_lt_hrs as decimal format ">9.99" label "Trans LT Hours".
define variable new-schd-recno as recid.
define variable old_db as character no-undo.
define variable sdb_err as integer no-undo.
define buffer posite for si_mstr.
define variable got_db like mfc_logical no-undo.
define variable use_calendardays as logical format "Calendar/Working"
   initial yes no-undo.
define variable firm_upto as date no-undo.
define variable l_tmp_string1 as character no-undo.
define variable l_tmp_string2 as character no-undo.
define variable l_gen_pln like mfc_logical
   label "Generate Planning Schedules" no-undo.
define variable l_gen_shp like mfc_logical
   label "Generate Shipping Schedules" no-undo.
define variable l_adg_module like mfc_logical initial false no-undo.
define variable l_rlse_id like sch_rlse_id no-undo.
define variable curr_rel_id as integer no-undo.
define variable msg-text like msg_desc extent 2 no-undo.
define variable days     as character format "x(80)" no-undo.

define variable l_chk_schmstr like mfc_logical no-undo.
define variable l_chk_schdet  like mfc_logical no-undo.

define new shared variable ship_dlvy_code as character.
define new shared variable interval_code as character.
define new shared variable req_dt as date.
define new shared variable req_qty as decimal.
define new shared variable week_offset as integer.
define new shared variable shipto_calid as character.
define new shared variable resch_ct as integer.
define new shared variable resch_dt as date extent 999 no-undo.
define new shared variable resch_qty as decimal extent 999 no-undo.
define new shared variable working_hours as decimal extent 999 no-undo.
define new shared variable resch_stat as character.
define new shared workfile work_schd like schd_det. /*workfiles last*/
define new shared variable date_based_rel_id like mfc_logical no-undo.
define new shared variable active_rlse_id    like sch_rlse_id no-undo.

define buffer current_sch_mstr for sch_mstr.
define buffer new_schd_det for schd_det.
define buffer poddet for pod_det.

define new shared temp-table t_schd_det no-undo like schd_det.
{pocnvars.i} /* Consignment variables */

days = getTermLabel("SUNDAY",10) + ",".
days = days + getTermLabel("MONDAY",10) + ",".
days = days + getTermLabel("TUESDAY",10) + ",".
days = days + getTermLabel("WEDNESDAY",10) + ",".
days = days + getTermLabel("THURSDAY",10) + ",".
days = days + getTermLabel("FRIDAY",10) + ",".
days = days + getTermLabel("SATURDAY",10).

FORM /*GUI*/ 
   po_nbr
   pod_part
   po_vend
   pod_site label "Ship To"
with STREAM-IO /*GUI*/  frame audit down width 80 no-attr-space.

/* SET EXTERNAL LABELS */
setFrameLabels(frame audit:handle).


/*GUI preprocessor Frame A define */
&SCOPED-DEFINE PP_FRAME_NAME A

FORM /*GUI*/ 
   
 RECT-FRAME       AT ROW 1 COLUMN 1.25
 RECT-FRAME-LABEL AT ROW 1 COLUMN 3 NO-LABEL VIEW-AS TEXT SIZE-PIXELS 1 BY 1
 SKIP(.1)  /*GUI*/
po_from              colon 16
   po_to                colon 46 label "To"
   part_from            colon 16
   part_to              colon 46 label "To"
   shipto_from          colon 16
   shipto_to            colon 46 label "To"
   supplier_from        colon 16
   supplier_to          colon 46 label "To"
   buyer_from           colon 16
   buyer_to             colon 46 label "To"
   skip(1)
   l_gen_pln            colon 35
   l_gen_shp            colon 35
   skip(1)
   asof_date            colon 35
   daylabel             no-label
   use_calendardays     colon 35 label "Calendar/Working Firm Days"
   report_yn            colon 35
   update_yn            colon 35
with frame a side-labels width 80 attr-space NO-BOX THREE-D /*GUI*/.

 DEFINE VARIABLE F-a-title AS CHARACTER INITIAL "".
 RECT-FRAME-LABEL:SCREEN-VALUE in frame a = F-a-title.
 RECT-FRAME-LABEL:HIDDEN in frame a = yes.
 RECT-FRAME:HEIGHT-PIXELS in frame a =
  FRAME a:HEIGHT-PIXELS - RECT-FRAME:Y in frame a - 2.
 RECT-FRAME:WIDTH-CHARS IN FRAME a = FRAME a:WIDTH-CHARS - .5.  /*GUI*/

/*GUI preprocessor Frame A undefine */
&UNDEFINE PP_FRAME_NAME



/* SET EXTERNAL LABELS */
setFrameLabels(frame a:handle).

asof_date = today.

assign
   l_gen_pln = false
   l_gen_shp = false.

/* The flag on the control file rsc_ctrl and mfc_ctrl would always
 * be maintained in sync. So we test value of mfc_ctrl flag */
l_adg_module = no.
if can-find (first mfc_ctrl where
                   mfc_field = "enable_shipping_schedules"
               and mfc_seq = 4
               and mfc_module = "ADG"
               and mfc_logical = yes)
then
   l_adg_module = yes.
else
   l_adg_module = no.

/* DETERMINE IF SUPPLIER CONSIGNMENT IS ACTIVE */
{gprun.i ""gpmfc01.p""
   "(input ENABLE_SUPPLIER_CONSIGNMENT,
     input 11,
     input ADG,
     input SUPPLIER_CONSIGN_CTRL_TABLE,
     output using_supplier_consignment)"}

repeat with frame a:

   hide frame audit no-pause.
   assign
      warn_ct = 0
      error_ct = 0.

   if po_to = hi_char then po_to = "".
   if part_to = hi_char then part_to = "".
   if shipto_to = hi_char then shipto_to = "".
   if supplier_to = hi_char then supplier_to = "".
   if buyer_to = hi_char then buyer_to = "".

   display
      string(entry(weekday(asof_date),days)) @ daylabel
      l_gen_pln
      l_gen_shp.

   update
      po_from
      po_to
      part_from
      part_to
      shipto_from
      shipto_to
      supplier_from
      supplier_to
      buyer_from
      buyer_to
      l_gen_pln when (l_adg_module)
      l_gen_shp when (l_adg_module)
      asof_date
      use_calendardays
      report_yn
      update_yn.

   display
      string(entry(weekday(asof_date),days)) @ daylabel.

   run mfquoter.

   if l_adg_module and not(l_gen_shp) and not(l_gen_pln) then do:
      /* NEED TO GENERATE AT LEAST PLANNING OR SHIPPING SCHEDULE */
      {pxmsg.i &MSGNUM=4376 &ERRORLEVEL=3}
      next-prompt l_gen_pln with frame a.
      undo, retry.
   end.

   if not(l_gen_shp) and l_gen_pln and weekday(asof_date) <> 2 then do:
      /* DATE MUST BE MONDAY WHEN GENERATING PLANNING SCHEDULES */
      {pxmsg.i &MSGNUM=4374 &ERRORLEVEL=3}
      next-prompt asof_date with frame a.
      undo, retry.
   end.

   if shipto_to = "" then shipto_to = hi_char.
   if part_to = "" then part_to = hi_char.
   if supplier_to = "" then supplier_to = hi_char.
   if buyer_to = "" then buyer_to = hi_char.
   if po_to = "" then po_to = hi_char.

   /* OUTPUT DESTINATION SELECTION */
   {gpselout.i &printType = "printer"
               &printWidth = 132
               &pagedFlag = " "
               &stream = " "
               &appendToFile = " "
               &streamedOutputToTerminal = " "
               &withBatchOption = "yes"
               &displayStatementType = 1
               &withCancelMessage = "yes"
               &pageBottomMargin = 6
               &withEmail = "yes"
               &withWinprint = "yes"
               &defineVariables = "yes"}
/*GUI*/ RECT-FRAME:HEIGHT-PIXELS in frame a = FRAME a:HEIGHT-PIXELS - RECT-FRAME:Y in frame a - 2.


   if not batchrun then do:
      if path <> "terminal" then do:
         output stream audit to terminal.
         clear frame audit all no-pause.
      end.
   end.

   {mfphead.i}

   do transaction on error undo, retry:
      /* DETERMINE IF DATE BASED RELEASES NEED TO BE USED */
      for first poc_ctrl no-lock:
      end.
      if available poc_ctrl then do:
         date_based_rel_id = poc__qadl06.
      end. /* if available poc_ctrl */
   end. /* do transaction */

   FORM /*GUI*/ 
      pod_site
      pod_part
      pod_um
      po_vend
      po_nbr
   with STREAM-IO /*GUI*/  frame summary down width 132 no-attr-space no-box.

   /* SET EXTERNAL LABELS */
   setFrameLabels(frame summary:handle).

   /* FIRST EDIT PO LINES TO ENSURE THEY HAVE ALLOCATION PERCENT
    * RECORDS DEFINED ON OR BEFORE ASOF DATE */

   for each pod_det /* exclusive */ no-lock
         where pod_site >= shipto_from and pod_site <= shipto_to
         and pod_part >= part_from and pod_part <= part_to
         and pod_nbr >= po_from and pod_nbr <= po_to
         and pod_sched
         use-index pod_part,
         each po_mstr no-lock
         where po_nbr = pod_nbr
         and po_vend >= supplier_from and po_vend <= supplier_to
         and po_buyer >= buyer_from and po_buyer <= buyer_to,
         each si_mstr no-lock
         where si_site = pod_site
         and si_db = global_db
         break by pod_site by pod_part by po_vend by po_nbr:

      /* SAVE THE ORDER'S ORIGINAL ORDER QTY*/
      for each wo_mstr exclusive-lock
            where wo_site = pod_site and wo_part = pod_part
            and wo_status = "P" and wo_qty_chg = 0:
         wo_qty_chg = wo_qty_ord.
      end.

      new-schd-recno = ?.

      l_tmp_string1 =   string(pod_nbr,"x(30)")
         + string(pod_part, "x(18)")
         + string(pod_site, "x(8)").
      l_tmp_string2 = l_tmp_string1 + hi_char.

      find last qad_wkfl where qad_key3 = "poa_det"
         and   qad_key4 >= l_tmp_string1
         and   qad_key4 <= l_tmp_string2
      no-lock no-error.

      /* NO ALLOCATION PERCENT SET UP FOR PO: #  LINE: #  ITEM: # */
      {pxmsg.i &MSGNUM=4294 &ERRORLEVEL=3 &MSGBUFFER=msg-text[1]
               &MSGARG1=pod_nbr
               &MSGARG2=pod_line
               &MSGARG3=pod_part}

      if not available qad_wkfl then do:
         put
            skip(1)

            msg-text[1]
            skip.
         error_ct = error_ct + 1.
         next.
      end.

      do for poddet transaction:

         find poddet where recid(poddet) = recid(pod_det)
         exclusive-lock.
         prior_end_date = ?.

         find scx_ref where scx_type = 2 and scx_order = pod_nbr
            and scx_line = pod_line no-lock.

         find code_mstr where code_fldname = "sch_sd_pat"
            and code_value = pod_sd_pat no-lock no-error.

         if report_yn then do:
            {gprun.i ""rsporpa.p""
               "(input recid(scx_ref),
                 input using_supplier_consignment)"}
         end.
         else do:
            display
               pod_site
               pod_part
               pod_um
               po_vend
               po_nbr
            with frame summary STREAM-IO /*GUI*/ .
            down 1 with frame summary.
         end.

         /* NOW WE CAN CREATE A NEW SCHEDULE */
         if date_based_rel_id then do:
            run determine_release_ID
               (input pod_nbr,
               input pod_line,
               output curr_rel_id).
         end. /* if date_based_rel_id then do */

         else do:
            if pod_rlse_nbr = 0 then pod_rlse_nbr = 1.
            repeat while can-find(sch_mstr where
                  sch_type = supp_schedule
                  and sch_nbr = pod_nbr
                  and sch_line = pod_line
                  and sch_rlse_id = string(pod_rlse_nbr,"999999")):
               pod_rlse_nbr = pod_rlse_nbr + 1.
               if pod_rlse_nbr = 999999 then pod_rlse_nbr = 1.
            end.
         end. /* else do: */

         /* FIND ACTIVE RELEASE-ID OF SHIPPING SCHEDULES (TYPE '6') */

         for last sch_mstr
            fields (sch_cr_date   sch_cr_time  sch_eff_end
                    sch_eff_start sch_fab_end  sch_fab_qty
                    sch_fab_strt  sch_line     sch_nbr
                    sch_pcr_qty   sch_pcs_date sch_raw_end
                    sch_raw_qty   sch_raw_strt sch_rlse_id
                    sch_sd_pat    sch_type)
            where sch_type = 4
            and   sch_nbr  = pod_nbr
            and   sch_line = pod_line
            and   sch_rlse_id begins l_rlse_id
            no-lock:
         end. /* FOR EACH sch_mstr */

         if not available sch_mstr
         then
            active_rlse_id = "".
         else
            active_rlse_id = sch_rlse_id.

         run find_active_rlse_id
            (input-output active_rlse_id,
             input 6,
             input pod_nbr,
             input pod_line).

         if supp_schedule = 4
         then do:
            l_chk_schmstr = yes.
            for first sch_mstr
               fields (sch_cr_date   sch_cr_time  sch_eff_end
                       sch_eff_start sch_fab_end  sch_fab_qty
                       sch_fab_strt  sch_line     sch_nbr
                       sch_pcr_qty   sch_pcs_date sch_raw_end
                       sch_raw_qty   sch_raw_strt sch_rlse_id
                       sch_sd_pat    sch_type)
               where sch_type    = supp_schedule
               and   sch_nbr     = pod_nbr
               and   sch_line    = pod_line
               and   sch_rlse_id = (if date_based_rel_id
                                    then
                                       l_rlse_id
                                    else
                                       string(pod_rlse_nbr,"999999")).
               l_chk_schmstr = no.
            end. /* FOR FIRST sch_mstr */
         end. /* IF supp_schedule = 4 */
         else
            l_chk_schmstr = yes.

         if l_chk_schmstr = yes
         then do:

            create sch_mstr.
            assign
               sch_type = supp_schedule
               sch_nbr = pod_nbr
               sch_line = pod_line
               sch_rlse_id = (if date_based_rel_id then l_rlse_id
               else string(pod_rlse_nbr,"999999")).

            if recid(sch_mstr) = -1 then .

         end. /* IF l_chk_schmstr = YES */

         /* GET CURRENT SCHEDULE IF THERE IS ONE AND COPY TO NEW SCHEDULE */
         do for current_sch_mstr:

            find current_sch_mstr
               where current_sch_mstr.sch_type = supp_schedule
               and current_sch_mstr.sch_nbr = sch_mstr.sch_nbr
               and current_sch_mstr.sch_line = sch_mstr.sch_line
               and current_sch_mstr.sch_rlse_id
               = pod_curr_rlse_id[supp_schedule - 3]
            exclusive-lock no-error.

            if available current_sch_mstr then do:

               /* NOTE: ONLY COPY DETAIL IF FIRM DAYS > 0 */
               {gprun.i ""rcsinit.p""
                  "(input recid(current_sch_mstr),
                    input recid(sch_mstr),
                    input (pod_firm_days > 0))"}

               current_sch_mstr.sch_eff_end = asof_date.

               if pod_firm_days > 0 then do:

                  /* SET THE NORMALLY EXPECTED END DATE FOR THE
                   * PRIOR SCHEDULE */
                  if use_calendardays then
                  prior_end_date
                  = current_sch_mstr.sch_pcs_date + pod_firm_days.
                  else do:
                     /* Using working days as firm days */
                     {gprun1.i ""mfhdate.p""
                        "(input current_sch_mstr.sch_pcs_date,
                          input pod_firm_days,
                          input scx_shipto,
                          true,
                          output prior_end_date)"}
                  end. /* else of if use_calendardays */

                  /* SET THE END DATE FOR THE  PRIOR  SCHEDULE  IN
                   * THE EVENT  THEY  ENTERED  A FIRM DATE PAST THE
                   * USUAL FIRM HORIZON */
                  find last schd_det
                     where schd_type = current_sch_mstr.sch_type
                     and schd_nbr = current_sch_mstr.sch_nbr
                     and schd_line = current_sch_mstr.sch_line
                     and schd_rlse_id = current_sch_mstr.sch_rlse_id
                     and schd_fc_qual = "f" no-lock no-error.

                  if available schd_det and schd_date > prior_end_date
                     then prior_end_date = schd_date.

                  /* GET RID OF EVERYTHING NOT FIRM */
                  for each schd_det exclusive-lock
                        where schd_type = sch_mstr.sch_type
                        and schd_nbr = sch_mstr.sch_nbr
                        and schd_line = sch_mstr.sch_line
                        and schd_rlse_id = sch_mstr.sch_rlse_id
                        and schd_fc_qual <> "f":
                     delete schd_det.
                  end.

               end.

               if report_yn then do:
                  {gprun.i ""rsiq1a.p""
                     "(input current_sch_mstr.sch_type,
                       input current_sch_mstr.sch_nbr,
                       input current_sch_mstr.sch_line,
                       input current_sch_mstr.sch_rlse_id,
                       input no,
                       input no)"} /* ACCOUNT FOR NEW INPUT PARAM */
               end.

            end.

         end.

         assign
            sch_cr_date   = today
            sch_cr_time   = time
            sch_sd_pat    = pod_sd_pat
            sch_pcs_date  = asof_date - 1
            sch_eff_start = asof_date.

         if sch_eff_end < sch_eff_start then sch_eff_end = ?.

         pod_curr_rlse_id[supp_schedule - 3] = sch_rlse_id.
         /* IF WE ARE IN A MULTI-DB ENVIRONMENT,  */
         /* SET POD_CURR_RLSE_ID IN REMOTE DB TOO */
         find posite where posite.si_site = pod_po_site
         no-lock no-error.
         if available posite then do:
            if posite.si_db <> global_db then do:
               old_db = global_db.
               {gprun.i ""gpalias3.p"" "(input posite.si_db,
                    output sdb_err)"}
               {gprun.i ""rssmt01.p"" "(input scx_po, input scx_line,
                    input sch_rlse_id, input supp_schedule)"}
               {gprun.i ""gpalias3.p"" "(input old_db, output sdb_err)"}
            end.
         end.

         if pod_firm_days = 0 then do:
            /* NEUTRALIZE PRIOR CUM REQUIRED */
            sch_pcr_qty = pod_cum_qty[1].
         end.

         /* GET WORKORDER DATA AND DROP INTO WORKFILE.  NOTE THAT
          * IF  prior_end_date  =  ?  THEN EITHER THERE WAS NO PRIOR
          * SCHEDULE OR WE HAVE FIRM DAYS = 0 IN WHICH CASE  WE  ACT
          * AS  THOUGH  THERE  IS NO PRIOR SCHEDULE SINCE WE REFRESH
          * EVERY TIME IN THAT CASE.  OTHERWISE prior_end_date  +  1
          * ACTS AS THE EARLIEST DATE TO PROCESS.  */
         {gprun.i ""rssupa.p""
            "(input recid(pod_det),
              input prior_end_date + 1,
              input asof_date,
              input report_yn,
              input-output warn_ct)"}

         /* DROP WORKFILE DATA INTO SCHEDULE */
         do for work_schd:

            for each work_schd no-lock:

               find schd_det where schd_det.schd_type = sch_type
                  and schd_det.schd_nbr = sch_nbr
                  and schd_det.schd_line = sch_line
                  and schd_det.schd_rlse_id = sch_rlse_id
                  and schd_det.schd_date = work_schd.schd_date
                  and schd_det.schd_time = ""
                  and schd_det.schd_interval = ""
                  and schd_det.schd_reference = ""
               exclusive-lock no-error.

               if not available schd_det then do:
                  create schd_det.
                  assign
                     schd_det.schd_type = sch_type
                     schd_det.schd_nbr  = sch_nbr
                     schd_det.schd_line = sch_line
                     schd_det.schd_rlse_id = sch_rlse_id
                     schd_det.schd_date = work_schd.schd_date
                     schd_det.schd_time = ""
                     schd_det.schd_interval = ""
                     schd_det.schd_reference = ""

                     schd_det.schd_fc_qual = "P"
                     schd_det.schd__chr01 = work_schd.schd__chr01.

                  if new-schd-recno = ? then
                     new-schd-recno = recid(schd_det).
               end.

               schd_det.schd_discr_qty =
               schd_det.schd_discr_qty + work_schd.schd_discr_qty.

            end.

         end.

         /* DO ORDER MULTIPLE ROUNDING.  NOTE THAT ANY FIRM  DATA
          * THAT   WAS  IN  THE  SCHEDULE  IS  LEFT  ALONE  BY  THIS
          * SUBPROGRAM.  THIS IS SO THE USER CAN OVERRIDE THE  ORDER
          * MULTIPLE IF THEY WANT.  */
         {gprun.i ""rcordmlt.p""
            "(input recid(sch_mstr), input pod_ord_mult,
              input report_yn)"}

         /* SET FIRM */
         if use_calendardays then
            firm_upto = sch_pcs_date + pod_firm_days .
         else do:
            /* Using working days as firm days */
            {gprun1.i ""mfhdate.p""
               "(input sch_pcs_date,
                 input pod_firm_days,
                 input scx_shipto,
                 true,
                 output firm_upto)"}
         end. /* else of if use_calendardays */

         if pod_firm_days > 0 then do:
            for each schd_det exclusive-lock
                  where schd_type = sch_type
                  and schd_nbr = sch_nbr
                  and schd_line = sch_line
                  and schd_rlse_id = sch_rlse_id
                  and schd_date <= firm_upto
                  and schd_fc_qual <> "f":
               schd_fc_qual = "F".
            end.
         end.

         /* ROLL FORWARD PRIOR CUM REQUIRED */
         for each schd_det exclusive-lock
               where schd_type = sch_type
               and schd_nbr = sch_nbr
               and schd_line = sch_line
               and schd_rlse_id = sch_rlse_id
               and schd_date < asof_date:

            if report_yn then do with frame schd-det:

               /* SET EXTERNAL LABELS */
               setFrameLabels(frame schd-det:handle).

               display
                  schd_date
                  schd_time
                  schd_reference
                  schd_discr_qty
                  schd_fc_qual
               with frame schd-det
                  title (getFrameTitle("DATES_PRIOR_TO_AS_OF_DATE_INC",78)) STREAM-IO /*GUI*/ .

               /* DETERMINE IF SUPPLIER PERFORMANCE IS INSTALLED */
               if can-find (mfc_ctrl where
                  mfc_field = "enable_supplier_perf"
                  and mfc_logical)
                  and can-find (_File where _File-name = "vef_ctrl")
               then do:
                  {gprunmo.i
                     &program= ""rssupve.p""
                     &module="ASP"
                     &param="""(input recid(schd_det))"""}
               end.  /* if enable supplier performance */

            end.

            sch_pcr_qty = sch_pcr_qty + schd_discr_qty.
            delete schd_det.

         end. /* for each schd_det */

         /* UPDATE CUM QUANTITIES */
         new_cum_qty = sch_pcr_qty.

         for each schd_det exclusive-lock
               where schd_type = sch_type
               and schd_nbr = sch_nbr
               and schd_line = sch_line
               and schd_rlse_id = sch_rlse_id:
            new_cum_qty = new_cum_qty + schd_discr_qty.
            schd_cum_qty = new_cum_qty.
         end.

         /* FOR SCHEDULES  WITH  FIRM,  IF  CUM  RECEIVED  STARTS
          * CONSUMING  PLANNED  ELEMENTS,  WE  NEED  TO INCREASE THE
          * FIRST PLANNED DATA VALUE SINCE MRP  WOULD  HAVE  ALREADY
          * NETTED OFF THE OVERSHIPMENT AGAINST IT.  */

         /*! ALTHOUGH THE SCHEDULE MAY HAVE BEEN CREATED AS PLANNED
          *  THE CODE ABOVE MAY HAVE SET IT TO FIRM.  WE NEED TO UPDATE
          *  THE FIRST "NEW" SCHEDULE DETAIL QUNATITY BY THE
          *  OVER SHIPMENT QUANTITY.*/

         if pod_firm_days > 0 then do:
            find schd_det where recid(schd_det) = new-schd-recno
            exclusive-lock no-error.

            if available schd_det then do:
               if pod_cum_qty[1] > schd_cum_qty - schd_discr_qty
                  then do with frame warn_frame:
                  schd_discr_qty = schd_discr_qty
                  + pod_cum_qty[1] - (schd_cum_qty - schd_discr_qty).

                  /* INCREASING 1st NEW SCHED DETAIL QTY FOR OVERSHIPMENT: # */
                  {pxmsg.i &MSGNUM=4295 &ERRORLEVEL=2 &MSGBUFFER=msg-text[1]
                           &MSGARG1=pod_nbr}

                  /* AND ADJUSTING OPEN QTY AGAIN FOR STANDARD PACK QTY */
                  {pxmsg.i &MSGNUM=4296 &ERRORLEVEL=1 &MSGBUFFER=msg-text[2]}

                  /* SET EXTERNAL LABELS */
                  setFrameLabels(frame warn_frame:handle).
                  display
                     skip(1)
                     msg-text[1] no-label
                     space(1)
                     pod_line label "PO Line"
                     pod_part label "PO Item"
                     skip
                     msg-text[2] no-label
                  with frame warn_frame side-labels width 132 STREAM-IO /*GUI*/ .

                  warn_ct = warn_ct + 1.

                  /* Since the above calculation can cause the open
                   * qty to become not properly divisible by the std
                   * pack qty, we do the rounding again */
                  {gprun.i ""rcordmlt.p"" "(input recid(sch_mstr),
                       input pod_ord_mult, input report_yn)"}

                  /* UPDATE CUM QUANTITIES AGAIN */
                  new_cum_qty = sch_pcr_qty.

                  for each schd_det exclusive-lock
                        where schd_type = sch_type
                        and schd_nbr = sch_nbr
                        and schd_line = sch_line
                        and schd_rlse_id = sch_rlse_id:
                     new_cum_qty = new_cum_qty + schd_discr_qty.
                     schd_cum_qty = new_cum_qty.
                  end.
               end.
            end.

         end.

         /* UPDATE RESOURCE AUTHORIZATIONS */
         assign
            sch_fab_strt = ?
            sch_fab_end = ?
            sch_fab_qty = 0
            sch_raw_strt = ?
            sch_raw_end = ?
            sch_raw_qty = 0.

         if pod_fab_days > 0 or pod_raw_days > 0 then do:
            if pod_fab_days > 0 then do:
               sch_fab_strt = pod_cum_date[1].
               sch_fab_end = asof_date + pod_fab_days - 1.
               sch_fab_qty = sch_pcr_qty.
            end.

            if pod_raw_days > 0 then do:
               sch_raw_strt = pod_cum_date[1].
               sch_raw_end = asof_date + pod_raw_days - 1.
               sch_raw_qty = sch_pcr_qty.
            end.

            for each schd_det no-lock
                  where schd_type = sch_type
                  and schd_nbr = sch_nbr
                  and schd_line = sch_line
                  and schd_rlse_id = sch_rlse_id:
               if pod_fab_days > 0 then do:
                  if ((schd_date - asof_date)) + 1 <= pod_fab_days
                  then do:

                     sch_fab_qty = schd_cum_qty.
                  end.
               end.

               if pod_raw_days > 0 then do:
                  if ((schd_date - asof_date)) + 1 <= pod_raw_days
                  then do:

                     sch_raw_qty = schd_cum_qty.
                  end.
               end.
            end.

         end.

         /* REPORT NEW SCHEDULE */
         if report_yn then do:
            {gprun.i ""rsiq1a.p""
               "(input sch_type, input sch_nbr, input sch_line,
                 input sch_rlse_id,
                 input no,
                 input no)"}  /* MODIFIED DUE TO NEW INPUT PARAMETER */
         end.

         /* FOLLOWING SECTION IS INTRODUTED FOR GENERATING THE     */
         /* PLANNING SCHEDULE WHEN ADG MODULE IS INSTALLED AND     */
         /* THE FLAG FOR GENERATING PLANNING SCHEDULE IS TURNED ON */
         if l_gen_pln and l_adg_module then
         do:
            {gprunmo.i
               &program= ""rssupc.p""
               &module="ASH"
               &param="""(input recid(pod_det),
                 input recid(sch_mstr),
                 input report_yn,
                 input asof_date)"""}
         end.  /* IF l_gen_pln AND pod__qad12 */

         /* FOLLOWING SECTION IS INTRODUCED FOR GENERATING THE     */
         /* SHIPPING SCHEDULE WHEN ADG MODULE IS INSTALLED AND     */
         /* THE FLAG FOR GENERATING SHIPPING SCHEDULE IS TURNED ON */
         if l_gen_shp and l_adg_module and pod__qad05 then
         do:
            {gprunmo.i
               &program= ""rssupd.p""
               &module="ASH"
               &param="""(input recid(pod_det),
                 input recid(sch_mstr),
                 input report_yn,
                 input use_calendardays,
                 input asof_date)"""}

         end.  /* IF l_gen_shp AND pod__qad05 */

         if report_yn then page.

         if not batchrun then do:

            if path <> "terminal" then do:
               pause 0.

               display stream audit
                  po_nbr
                  pod_part
                  po_vend
                  pod_site label "Ship To"
               with frame audit STREAM-IO /*GUI*/ .

               pause 0.
               down stream audit 1 with frame audit.
            end.

         end.

         /* UPDATE MRP */
         if update_yn then do:
            {gprun.i ""rsmrw.p""
               "(input pod_nbr, input pod_line, input yes)"}
         end.

         run subtract_firmqty
            (input recid(pod_det),
            input recid(sch_mstr),
            input recid(scx_ref)).

         if not update_yn then undo, next.

      end. /* pod_det trans */

   end. /* error_ct = 0 ---> for each */

   {mfrtrail.i}

   if not batchrun then do:
      if path <> "terminal" then do:
         output stream audit close.
      end.
   end.

   if warn_ct > 0 then do:
      /* WARNING MESSAGES PRODUCED */
      run display_message_mfmsg02
         (input 8222,
          input 2,
          input warn_ct).
   end.
   if error_ct > 0 then do:
      /* ERROR MESSAGES PRODUCED */
      run display_message_mfmsg02
         (input 8234,
          input 2,
          input error_ct).
   end.

end.

/* Procedure to update batch run parameter bcdparam */
PROCEDURE mfquoter:

   bcdparm = "".
   {mfquoter.i po_from}
   {mfquoter.i po_to}
   {mfquoter.i part_from}
   {mfquoter.i part_to}
   {mfquoter.i shipto_from}
   {mfquoter.i shipto_to}
   {mfquoter.i supplier_from}
   {mfquoter.i supplier_to}
   {mfquoter.i buyer_from}
   {mfquoter.i buyer_to}
   if l_adg_module then
   do:
      {mfquoter.i l_gen_pln}
      {mfquoter.i l_gen_shp}
   end. /* IF L_ADG_MODULE */
   {mfquoter.i asof_date}
   {mfquoter.i use_calendardays}
   {mfquoter.i report_yn}
   {mfquoter.i update_yn}

END PROCEDURE.

PROCEDURE subtract_firmqty:
   define input parameter pod_recid as recid no-undo.
   define input parameter sch_recid as recid no-undo.
   define input parameter scx_recid as recid no-undo.

   for first pod_det where recid(pod_det) = pod_recid:
   end.

   for first sch_mstr where recid(sch_mstr) = sch_recid:
   end.

   for first scx_ref where recid(scx_ref) = scx_recid:
   end.

   if pod_firm_days > 0
      and pod_type = ""
      and update_yn then do:

      if use_calendardays then
         firm_upto = sch_pcs_date + pod_firm_days .
      else do:
         /* Using working days as firm days */
         {gprun1.i ""mfhdate.p""
            "(input sch_pcs_date,
              input pod_firm_days,
              input scx_shipto,
              true,
              output firm_upto)"}
      end. /* else of if use_calendardays */
      {gprun.i ""rssupb.p"" "(input firm_upto)"}
   end.

END PROCEDURE.  /* Procedure subtract_firmqty */

PROCEDURE determine_release_ID:
   define input parameter in_pod_nbr like pod_nbr no-undo.
   define input parameter in_pod_line like pod_line no-undo.
   define output parameter cur_rel_id like sch_rlse_id no-undo.

   curr_rel_id = 0.

   l_rlse_id = string(year(asof_date), "9999") +
      string(month(asof_date), "99")  +
      string(day(asof_date), "99")    + "-".

   for last sch_mstr where
      sch_type = 4            and
      sch_nbr = in_pod_nbr       and
      sch_line = in_pod_line     and
      sch_rlse_id begins l_rlse_id no-lock:
   end.

   if not available sch_mstr then do:
      l_rlse_id = l_rlse_id + string(001,"999").
   end.
   else do:
      curr_rel_id = integer(substring(sch_rlse_id,11,3)).

      if curr_rel_id + 1 > 999 then do:
         l_rlse_id = l_rlse_id + string(001,"999").
      end.
      else do:
         l_rlse_id = l_rlse_id +
         string(integer(substring(sch_rlse_id,11,3)) + 1,"999").
      end.
   end. /* else do: */

END PROCEDURE.

PROCEDURE display_message_mfmsg02:
   define input parameter l_msg_nbr as integer no-undo.
   define input parameter l_msg_level as integer no-undo.
   define input parameter l_parm_1 as integer no-undo.

   {pxmsg.i &MSGNUM=l_msg_nbr &ERRORLEVEL=l_msg_level &MSGARG1=l_parm_1}

END PROCEDURE.

PROCEDURE find_active_rlse_id:

   define input-output parameter act_rlse_id like sch_rlse_id no-undo.

   define input parameter act_sch_type like sch_type  no-undo.
   define input parameter act_pod_nbr  like pod_nbr   no-undo.
   define input parameter act_pod_line like pod_line  no-undo.

   define buffer b_schmstr for sch_mstr.
   define buffer b_poddet  for pod_det.

   for first b_poddet
       fields (pod_cum_date pod_cum_qty   pod_curr_rlse_id
               pod_fab_days pod_firm_days pod_line
               pod_nbr      pod_ord_mult  pod_part
               pod_po_site  pod_raw_days  pod_rlse_nbr
               pod_sched    pod_sd_pat    pod_site
               pod_type     pod_um        pod__qad05)
      where b_poddet.pod_nbr  = act_pod_nbr
      and   b_poddet.pod_line = act_pod_line
      no-lock :

      if b_poddet.pod_curr_rlse_id[act_sch_type - 3] <> ""
      then
         act_rlse_id = b_poddet.pod_curr_rlse_id[act_sch_type - 3].

      else do:
         for first b_schmstr
            fields (sch_cr_date   sch_cr_time  sch_eff_end
                    sch_eff_start sch_fab_end  sch_fab_qty
                    sch_fab_strt  sch_line     sch_nbr
                    sch_pcr_qty   sch_pcs_date sch_raw_end
                    sch_raw_qty   sch_raw_strt sch_rlse_id
                    sch_sd_pat    sch_type)
            where b_schmstr.sch_type    = act_sch_type
            and   b_schmstr.sch_nbr     = act_pod_nbr
            and   b_schmstr.sch_line    = act_pod_line
            and   b_schmstr.sch_eff_end = ?
            no-lock:

            act_rlse_id = b_schmstr.sch_rlse_id.

         end. /* FOR FIRST b_schmstr */

      end. /* ELSE DO */

   end. /* FOR FIRST b_poddet */

END PROCEDURE. /* PROCEDURE find_active_rlse_id */
